{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def compute_p_and_r(pred, target):\n",
    "    # compute the p and r of the foreground\n",
    "    # batchsize = 1\n",
    "    TP = np.sum(target * pred)\n",
    "    p = TP / np.sum(pred)\n",
    "    r = TP / np.sum(target)\n",
    "    # print('TP, p ,r:',TP, p ,r)\n",
    "        \n",
    "    return p, r\n",
    "\n",
    "def compute_PWC(pred, target):\n",
    "    # first: compute TP_TN_FP_FN\n",
    "    TP = np.sum(target * pred)\n",
    "    FP = np.sum(pred) - TP\n",
    "    FN = np.sum(target) - TP\n",
    "    # TN = target.shape[0] * target.shape[1] - TP - FP - FN\n",
    "\n",
    "    PWC = 100 * (FN + FP) / (target.shape[0] * target.shape[1])\n",
    "\n",
    "    # print('TP, p ,r:',TP, p ,r)\n",
    "    \n",
    "    return PWC\n",
    "\n",
    "\n",
    "def compute_F_score_and_PWC_of_middle_fuse(pred, target):\n",
    "    # F_score_list = []\n",
    "    # thresh_list = []\n",
    "    # print('max, min:', pred.max(), pred.min())\n",
    "    # print('np.sum(target):', np.sum(target))\n",
    "\n",
    "    if np.sum(target) != 0:\n",
    "        thresh = 230\n",
    "        # print('thresh:', thresh)\n",
    "        pred_ = np.where(pred > np.uint8(thresh), np.uint8(1), np.uint8(0))\n",
    "        p_, r_ = compute_p_and_r(pred_, target)\n",
    "        PWC = compute_PWC(pred_, target)\n",
    "        if (p_ == 0) or (r_ == 0):\n",
    "            return None, None, None, None, None\n",
    "        F_score = 2 * p_ * r_ / (p_ + r_)\n",
    "        # print('F_score_thresh,p,r,F_score:',thresh, p_, r_, F_score)\n",
    "\n",
    "        return F_score, thresh, p_, r_, PWC\n",
    "    else:\n",
    "        return None, None, None, None, None\n",
    "\n",
    "def compute_F_score_and_PWC_of_final(pred, target):\n",
    "    # F_score_list = []\n",
    "    # thresh_list = []\n",
    "    # print('max, min:', pred.max(), pred.min())\n",
    "    # print('np.sum(target):', np.sum(target))\n",
    "\n",
    "    if np.sum(target) != 0:\n",
    "        thresh = 230 # general:255, nightvideos:243\n",
    "        # print('thresh:', thresh)\n",
    "        pred_ = np.where(pred > np.uint8(thresh), np.uint8(1), np.uint8(0))\n",
    "        p_, r_ = compute_p_and_r(pred_, target)\n",
    "        PWC = compute_PWC(pred_, target)\n",
    "        if (p_ == 0) or (r_ == 0):\n",
    "            return None, None, None, None, None\n",
    "        F_score = 2 * p_ * r_ / (p_ + r_)\n",
    "        # print('F_score_thresh,p,r,F_score:',thresh, p_, r_, F_score)\n",
    "\n",
    "\n",
    "        return F_score, thresh, p_, r_, PWC\n",
    "    else:\n",
    "        return None, None, None, None, None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " F_score, thresh, p, r, PWC = util.compute_F_score_and_PWC_of_middle_fuse(fuse_image, targets.data.cpu().numpy()[0][0])\n",
    "\n",
    "        all_F_score.append(F_score)\n",
    "        all_thresh.append(thresh)\n",
    "        all_p.append(p)\n",
    "        all_r.append(r)\n",
    "        all_PWC.append(PWC)\n",
    "    #\n",
    "        ######################### final ##########################\n",
    "        F_score_final, thresh_final, p_final, r_final, PWC_final = util.compute_F_score_and_PWC_of_final(final_distance_map * roi.data.cpu().numpy()[0][0] * 255, targets.data.cpu().numpy()[0][0])\n",
    "\n",
    "        all_F_score_final.append(F_score_final)\n",
    "        all_thresh_final.append(thresh_final)\n",
    "        all_p_final.append(p_final)\n",
    "        all_r_final.append(r_final)\n",
    "        all_PWC_final.append(PWC_final)\n",
    "    #\n",
    "    #     # if F_score != None:\n",
    "    #     #     # print('Good_Pic')\n",
    "    #     #     print(F_score, thresh)\n",
    "    #     #     save_weight_fig_dir = save_weight_fig_dir.replace('.jpg', '_binary.jpg')\n",
    "    #     #     cv2.imwrite(save_weight_fig_dir, np.where(fuse_image >= np.uint8(thresh), np.uint8(255), np.uint8(0)))\n",
    "    # ################### fuse ###################\n",
    "    print('############## mean_F_score ##################')\n",
    "    util.compute_mean(all_F_score)\n",
    "    print('################# mean_p #####################')\n",
    "    util.compute_mean(all_p)\n",
    "    print('################# mean_r #####################')\n",
    "    util.compute_mean(all_r)\n",
    "    print('################# mean_PWC ###################')\n",
    "    util.compute_mean(all_PWC)\n",
    "    #\n",
    "    ################### final #################\n",
    "    print('############## mean_F_score_of_final ##################')\n",
    "    util.compute_mean(all_F_score_final)\n",
    "    print('############## mean_p_of_final ##################')\n",
    "    util.compute_mean(all_p_final)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
